package com.geek.controller;

import com.geek.bean.BaseRespVo;
import com.geek.bean.PageData;
import com.geek.bean.data.GoodsCollectData;
import com.geek.bean.vo.CollectVO;
import com.geek.service.CollectService;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx/collect")
@Api(tags = "收藏模块")
public class CollectController {


    @Autowired
    private CollectService collectService;

    @GetMapping("list")
    public BaseRespVo list(Byte type, Integer page, Integer limit) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        List<GoodsCollectData> collectList = collectService.list(userId, type, page, limit);
        return BaseRespVo.ok(PageData.list(collectList));
    }

    @PostMapping("addordelete")
    public BaseRespVo addOrDelete(@RequestBody CollectVO collect) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            return BaseRespVo.fail("请先登录", 501);
        }
        Integer userId = (Integer) subject.getPrincipal();
        Byte type = collect.getType();
        Integer valueId = collect.getValueId();
        collectService.andOrDeleteGoods(userId, type, valueId);
        return BaseRespVo.ok();
    }

}